package msu.ml.data;

import msu.ml.core.*;
import msu.ml.core.NxInstances;
import weka.core.*;

/**
 * This class serves as an adapter allowing the Remove filter in Weka to be used
 * inside the Nexrad Explorer Software.
 *
 * @author Reginald M Mead
 * @version 1.0
 * @see IDataPreProcessor
 */
public class AbsoluteValuePreProcessor implements IDataPreProcessor 
{
   /**
    * Create a new Attribute Removal Preprocessor
    */
   public AbsoluteValuePreProcessor()
   {

   }

   private boolean enabled = true;
   public boolean getEnabled(){return enabled;}
   public void setEnabled(boolean value){this.enabled = value;}

   private String attribute;
   public void setAttribute(String attribute)
   {
      this.attribute = attribute;
   }

   /**
    * Processes data by removing or keeping the specified attributes
    * @param data data to process
    * @return processed data
    */
   public NxInstances process(NxInstances data)
   {
      if(enabled)
      {
         try
         {
            int attIndex = Integer.parseInt(this.attribute) - 1;
            for(int i = 0; i < data.numInstances(); i++)
            {
               Instance inst = data.instance(i);
               inst.setValue(attIndex, (double)Math.abs(
                  inst.value(attIndex)));
            }
         }
         catch(Exception e)
         {
            System.out.println(e);
            return null;
         }
      }
      return data;
   }

   public String toString()
   {
      return "<absolute_value attribute=\"" + 
         this.attribute + "\" />";
   }

}
